package com.java.shop.order.inteceptor;

import com.java.common.constant.LoginUserConstant;
import com.java.common.to.MemberTo;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


public class OrderInteceptor implements HandlerInterceptor {
    public static ThreadLocal<MemberTo> threadLocal = new ThreadLocal<>();
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String uri = request.getRequestURI();
        String s = request.getRequestURL().toString();
        AntPathMatcher antPathMatcher = new AntPathMatcher();
        boolean match = antPathMatcher.match("/order/order/getInfo/**", uri);
        boolean match1 = antPathMatcher.match("/payed/**", uri);
        if(match || match1) return true;

        MemberTo memberTo = (MemberTo)request.getSession().getAttribute(LoginUserConstant.LOGIN_USER);
        if(memberTo != null){
            threadLocal.set(memberTo);
            return true;
        }else{
            response.sendRedirect("http://auth.shop.com/login.html");
            return false;
        }
    }

}
